/*
 * $RCSfile: Processor_57_AddChips.java,v $
 * $Revision: 1.1  $
 * $Date: 2012-8-17  $
 *
 * Copyright (C) 2005 Bettem, Inc. All rights reserved.
 *
 * This software is the proprietary information of Bettem, Inc.
 * Use is subject to license terms.
 */
package com.i366.processor;

import java.sql.SQLException;
import java.util.Map;

import org.apache.log4j.Logger;

import com.i366.Constant;
import com.i366.cache.Cache;
import com.i366.room.model.impl.Room;
import com.i366.service.RoomService;
import com.work.client.util.I366ClientPickUtil;
import com.work.comm.util.I366PickUtil;
import com.work.db.UserInfoDao;
import com.work.db.imp.UserInfoDaoImp;
import com.work.db.model.UserInfo;
import com.work.engine.protocal.request.Request;
import com.work.engine.server.handler.Handler;

/**
 * 添加筹码
 *
 * <p>Title: Processor_57_AddChips</p> 
 * <p>Description: </p> 
 * <p>Copyright: Copyright (c) 2006</p> 
 * @author shenxing.ruan
 * @version 1.0
 */
public class Processor_57_AddChips extends Handler{

	private Logger logger = Logger.getLogger(Processor_57_AddChips.class);
	public byte[] handleRequest() {
		logger.debug("Processor_54_TimeoutBack userId is : " + this.getGameRequest().getUserId());
		//=======================相关验证=================
		Request request = getGameRequest();
		RoomService.updateUser(request);
		int[][] int2 = {
				{60,I366ClientPickUtil.TYPE_INT_1},
				{130,I366ClientPickUtil.TYPE_INT_4},
		};
		Map<Integer, Object> map = I366ClientPickUtil.pickAll(request.getBt(), int2);
		int seatId = (Integer)map.get(60);
		int chips = (Integer)map.get(130); //需要添加的筹码
		//
		try {
			UserInfoDao userInfoDao = new UserInfoDaoImp();
			UserInfo userInfo2 = null;
			try {
				userInfo2 = userInfoDao.getUserInfo(request.getUserId());
			} catch (SQLException e) {
				logger.debug(e);
				return null;
			}
			UserInfo userInfo = Cache.getOnlineUserInfo().get(request.getUserId());
			if (userInfo == null) {
				return pusUser(1);
			}
			//重数据库中添加了多少筹码
			int newUpChouma = userInfo2.getChip() - userInfo.getChip();
			if (newUpChouma == 0) {
				logger.debug("数据库中没有筹码！");
			}
			userInfo.setChip(userInfo2.getChip());

			Room room = Cache.getRoom(userInfo.getRoomId(), userInfo.getRoomPath());
			if (room == null) {
				return pusUser(1);
			}
			int i = room.getRoomService().addChip(request.getUserId(), newUpChouma, chips);
			if (i == 1) {
				return pusUser(1);
			}
			return null;
		} catch (Exception e) {
			return pusUser(1);
		}
	}

	public Object makeObject() throws Exception {
		return new Processor_57_AddChips();
	}
	public byte[] pusUser(int status) {
		Object[][] objs2 = {
				{61,status,I366ClientPickUtil.TYPE_INT_1}
		};
		byte[] bytes = I366PickUtil.packAll(objs2, Constant.REQ_GAME_RECV_ADD_CHIPS);
		return bytes;
	}
}

